package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.CornerPathEffect
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* GameView
* */
class GameView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val cornerValue = context.resources.getDimension(R.dimen.dp_10)

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            style = Paint.Style.STROKE
            pathEffect = CornerPathEffect(cornerValue)
            color = context.resources.getColor(R.color.color_83CF39)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_83CF39)
        }
    }

    private val mPath by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            save()
            translate(cx, cy + cornerValue / 4)

            val mStrokeWidth = (cx / 13).coerceAtLeast(minStrokeWidth)
            mPaint0.strokeWidth = mStrokeWidth
            mPaint1.strokeWidth = mStrokeWidth

            mPath.apply {
                reset()
                moveTo(-cx * 5 / 12, cy * 5 / 12)
                lineTo(-cx * 11 / 12, cy * 11 / 12)
                lineTo(-cx * 11 / 12, -cy * 11 / 12)
                lineTo(cx * 11 / 12, -cy * 11 / 12)
                lineTo(cx * 11 / 12, cy * 11 / 12)
                lineTo(cx * 5 / 12, cy * 5 / 12)
            }
            drawPath(mPath, mPaint0)

            mPath.apply {
                reset()
                moveTo(-cx * 5 / 12, cy * 5 / 12)
                lineTo(cx * 5 / 12, cy * 5 / 12)
            }
            drawPath(mPath, mPaint1)

            mPath.apply {
                reset()
                moveTo(-cx * 5 / 12, cy * 2 / 12)
                lineTo(-cx * 5 / 12, -cy * 6 / 12)

                moveTo(-cx * 1 / 12, -cy * 2 / 12)
                lineTo(-cx * 9 / 12, -cy * 2 / 12)
            }
            drawPath(mPath, mPaint1)

            mPath.apply {
                reset()

                moveTo(cx * 1 / 12 + mStrokeWidth + cx / 8, -cy * 1 / 12 - mStrokeWidth)
                lineTo(cx * 4 / 12 + mStrokeWidth + cx / 8, -cy * 4 / 12 - mStrokeWidth)

                moveTo(cx * 1 / 12 + mStrokeWidth + cx / 4, -cy * 1 / 12 - mStrokeWidth + cy / 8)
                lineTo(cx * 4 / 12 + mStrokeWidth + cx / 4, -cy * 4 / 12 - mStrokeWidth + cy / 8)
            }
            drawPath(mPath, mPaint1)

            restore()
        }
    }
}